package com.zhang.bingmei_incomeexpenditure.entity.ie;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zhang.bingmei_incomeexpenditure.entity.commom.PageDTO;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;


@Data
@Accessors(chain = true)
//@ApiModel(description = "收支管理页面查询DTO")
public class IncomeExpenditurePageDTO extends PageDTO {

    //@ApiModelProperty(value = "开始日期",required = true)
    @NotNull(message = "开始日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date startDate;

    //@ApiModelProperty(value = "结束日期",required = true)
    @NotNull(message = "结束日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date endDate;

    // 格式化后的日期字符串（用于Excel填充，如"2024年01月01日"）
    private String startDateStr;
    private String endDateStr;

    /**
     * 初始化默认日期：（保险起见）
     * - 若startDate为空，设置为2024年1月1日
     * - 若endDate为空，设置为当前年份12月31日
     */
    public void initDefaultDates() {
        // 处理开始时间
        ifEmptyCreateStartDate();

        // 处理结束时间
        ifEmptyCreateEnddate();
    }

    public boolean hasEmptyDates() {
        return startDate == null || endDate == null;
    }

    public Integer getStartYear() {
        ifEmptyCreateStartDate();
        return Integer.parseInt(new SimpleDateFormat("yyyy").format(startDate));
    }

    public Integer getEndYear() {
        // 处理结束时间
        ifEmptyCreateEnddate();
        return Integer.parseInt(new SimpleDateFormat("yyyy").format(endDate));
    }

    private void ifEmptyCreateStartDate() {
        if (this.startDate == null) {
            LocalDate startDate = LocalDate.of(2024, 1, 1);
            this.startDate = Date.from(startDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        }
    }

    private void ifEmptyCreateEnddate() {
        if (this.endDate == null) {
            int currentYear = LocalDate.now().getYear();
            LocalDate endDate = LocalDate.of(currentYear, 12, 31);
            LocalDateTime endDateTime = endDate.atTime(23, 59, 59);
            this.endDate = Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant());
        }
    }
}